package com.spa.infrastructure.repository.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.spa.infrastructure.enums.dict.city.CityStutsEnum;
import com.spa.infrastructure.repository.condition.CitiesServedCondition;
import com.spa.infrastructure.repository.mapper.CitiesServedMapper;
import com.spa.infrastructure.repository.po.CitiesServedPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * domain service
 * 开通城市管理(CitiesServed) 
 *
 * @author makejava
 * @since 2024-01-12 15:52:51
 */
@Component
public class CitiesServedRepositoryService extends MPJBaseServiceImpl<CitiesServedMapper, CitiesServedPO> {

    @Autowired
    private CitiesServedMapper citiesServedMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public CitiesServedPO queryById(Long id) {
		return getById(id);
    }

    public void add(CitiesServedPO po) {
        po.setStatus(CityStutsEnum.$20.getCode());
        po.setOpenTime(new Date());
        citiesServedMapper.insert(po);
    }

    public boolean existsCity(CitiesServedCondition condition) {
        return exists(wrapper(condition));
    }

	/**
     * 分页查询
     * @return 查询结果
     */
	public Page<CitiesServedPO> pageListInfo(CitiesServedCondition condition) {
		LambdaQueryWrapper<CitiesServedPO> wrapper = wrapper(condition);
        return page(Page.of(condition.getCurrent(), condition.getSize()), wrapper);

    }
	
	public List<CitiesServedPO> listInfo(CitiesServedCondition condition) {
        return list(wrapper(condition));
    }

    public void updateCity(CitiesServedPO po) {
        update(null, updateWrapper(po));
    }

	private LambdaQueryWrapper<CitiesServedPO> wrapper(CitiesServedCondition condition) {
        LambdaQueryWrapper<CitiesServedPO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotNull(condition.getId()),CitiesServedPO::getId, condition.getId());
        wrapper.eq(ObjectUtil.isNotNull(condition.getStatus()),CitiesServedPO::getStatus, condition.getStatus());
        wrapper.eq(ObjectUtil.isNotNull(condition.getCityName()),CitiesServedPO::getCityName, condition.getCityName());
        wrapper.eq(ObjectUtil.isNotNull(condition.getRegion()),CitiesServedPO::getRegion, condition.getRegion());
        wrapper.in(CollUtil.isNotEmpty(condition.getIds()), CitiesServedPO::getId, condition.getIds());
        wrapper.orderByDesc(CitiesServedPO::getOpenTime);
        return wrapper;
    }

    private LambdaUpdateWrapper<CitiesServedPO> updateWrapper(CitiesServedPO po) {
        LambdaUpdateWrapper<CitiesServedPO> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(CitiesServedPO::getId, po.getId());
        updateWrapper.set(ObjectUtil.isNotNull(po.getStatus()), CitiesServedPO::getStatus, po.getStatus());
        updateWrapper.set(ObjectUtil.isNotNull(po.getDistance()), CitiesServedPO::getDistance, po.getDistance());
        updateWrapper.set(ObjectUtil.isNotNull(po.getPrice()), CitiesServedPO::getPrice, po.getPrice());
        updateWrapper.set(ObjectUtil.isNotNull(po.getIncrementPrice()), CitiesServedPO::getIncrementPrice, po.getIncrementPrice());
        return updateWrapper;
    }
}
